package com.pn.warehouse.controller;

import com.pn.warehouse.entity.*;
import com.pn.warehouse.page.Page;
import com.pn.warehouse.service.*;
import com.pn.warehouse.utils.TokenUtils;
import com.pn.warehouse.utils.WarehouseConstants;
import io.netty.util.internal.ResourcesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/product")
@RestController
public class ProductController {

    @Autowired
    private StoreService storeService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductTypeService productTypeService;

    @Autowired
    private SupplyService supplyService;

    @Autowired
    private PlaceService placeService;

    @Autowired
    private UnitService unitService;

    @Autowired
    private TokenUtils tokenUtils;

    //将配置文件中的file.upload-path值注入给属性 -- 图片上传的位置
    @Value("${file.upload-path}")
    public String uploadPath;

    /**
     * 查询所有仓库
     */
    @RequestMapping("/store-list")
    public Result storeList(){
        //1、执行业务并返回
        List<Store> allStore = storeService.findAllStore();
        return Result.ok(allStore);
    }

    /**
     * 查询所有品牌
     */
    @RequestMapping("/brand-list")
    public Result brandList(){
        //1、执行业务并返回
        List<Brand> allBrand = brandService.findAllBrand();
        return Result.ok(allBrand);
    }

    /**
     * 分页查询商品列表
     */
    @RequestMapping("/product-page-list")
    public Result productListPage(Page page, Product product){
        //1、执行业务并返回
        page = productService.queryProductPage(page, product);
        return Result.ok(page);
    }

    /**
     * 查询所有商品的分类树
     */
    @RequestMapping("/category-tree")
    public Result loadTypeTree(){
        //1、执行业务并返回
        List<ProductType> productTypes = productTypeService.productTypeTree();
        return Result.ok(productTypes);
    }

    /**
     * 查询所有供应商
     */
    @RequestMapping("/supply-list")
    public Result supplyList(){
        //1、执行业务并返回
        List<Supply> allSupply = supplyService.findAllSupply();
        return Result.ok(allSupply);
    }

    /**
     * 查询所有的产地
     */
    @RequestMapping("/place-list")
    public Result placeList(){
        //1、执行业务并返回
        List<Place> allPlace = placeService.findAllPlace();
        return Result.ok(allPlace);
    }

    /**
     * 查询所有的单位
     */
    @RequestMapping("/unit-list")
    public Result unitList(){
        //1、执行业务并返回
        List<Unit> allUnit = unitService.findAllUnit();
        return Result.ok(allUnit);
    }

    /**
     *图片上传
     * @param file ：MultipartFile -- 封装了需要上传的图片文件
     */
    @CrossOrigin //表示该请求允许被跨域请求
    @RequestMapping("/img-upload")
    public Result uploadImage(MultipartFile file){
        try {
            //拿到图片上传目录路径的File对象
            File uploadFile = ResourceUtils.getFile(uploadPath);
            //拿到图片上传到的目录路径的磁盘路径
            String uploadDirPath = uploadFile.getAbsolutePath();
            //拿到上传的图片的名称
            String filename = file.getOriginalFilename();
            //拿到上传的文件要保存到磁盘文件的路径
            file.transferTo(new File(uploadDirPath + "/" + filename));

            return Result.ok("图片上传成功！");
        } catch (Exception e) {

            return Result.err(Result.CODE_ERR_BUSINESS, "图片上传失败！");
        }
    }

    /**
     * 添加商品
     */
    @RequestMapping("/product-add")
    public Result addProduct(@RequestBody Product product, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        //1、拿到当前用户的id
        int userId = tokenUtils.getCurrentUser(token).getUserId();

        //2、设置创建人
        product.setCreateBy(userId);

        //3、执行业务并返回
        Result result = productService.insertProduct(product);
        return result;
    }

    /**
     * 修改商品上下架状态
     * @param product
     * @return
     */
    @RequestMapping("/state-change")
    public Result changeProductState(@RequestBody Product product){
        //1、执行业务并返回
        Result result = productService.updateStateById(product);
        return result;
    }

    /**
     * 删除单个商品
     */
    @RequestMapping("/product-delete/{productId}")
    public Result deleteProduct(@PathVariable Integer productId){
        //1、执行业务并返回
        Result result = productService.removeProductById(Arrays.asList(productId));
        return result;
    }

    /**
     * 批量删除商品
     */
    @RequestMapping("/product-list-delete")
    public Result deleteProductList(@RequestBody List<Integer> productIdList){
        //1、执行业务并返回
        Result result = productService.removeProductById(productIdList);
        return result;
    }

    /**
     * 修改商品
     */
    @RequestMapping("/product-update")
    public Result updateProduct(@RequestBody Product product, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        //1、拿到当前用户的id
        int userId = tokenUtils.getCurrentUser(token).getUserId();

        //2、设置创建人
        product.setUpdateBy(userId);

        //3、执行业务并返回
        Result result = productService.updateProductById(product);
        return result;
    }

}
